#include "config.h"
#include "controller.h"
#include "iot2.h"
#include "at_com.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

#include "string.h"

extern WAIT_OK g_wait_ok;
extern QueueHandle_t iot_rx_topic_que;

uint8_t g_is_sub_topic = pdFALSE;

void service_task_entry(void *pvParameters)
{
    (void)pvParameters;
    int ret = 0;
    char func_name[32] = {0};
    vTaskDelay(pdMS_TO_TICKS(3000));
//    USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
    for ( ;; ) 
    {
      // 1.
      at_send_CWJAP(WIFI_SSID, WIFI_PWD);
      sprintf(func_name, "at_send_CWJAP");
    	while ((ret = waiting_OK(5000)) != AT_ERRNO_OK) {
          switch (ret) {
            case AT_ERRNO_TIMEOUT:
              printf("%s timeout\r\n", func_name);
              break;
            case AT_ERRNO_ERROR:
              printf("%s error\r\n", func_name);
              break;
            default:
              printf("unknown ret:%d\r\n", ret);
              break;
          }
          vTaskDelay(pdMS_TO_TICKS(3000));
          at_send_CWJAP(WIFI_SSID, WIFI_PWD);
        }
        reset_wait_OK(&g_wait_ok); 
        vTaskDelay(pdMS_TO_TICKS(1000));

        // 2.
        ret = 0;
        at_send_MQTTUSERCFG("ESP01");
        sprintf(func_name, "at_send_MQTTUSERCFG");
        while ((ret = waiting_OK(5000)) != AT_ERRNO_OK) {
          switch (ret) {
            case AT_ERRNO_TIMEOUT:
              printf("%s timeout\r\n", func_name);
              break;
            case AT_ERRNO_ERROR:
              printf("%s error\r\n", func_name);
              break;
            default:
              printf("unknown ret:%d\r\n", ret);
              break;
          }
          vTaskDelay(pdMS_TO_TICKS(3000));
          at_send_MQTTUSERCFG("ESP01");
        }
        reset_wait_OK(&g_wait_ok); 
        vTaskDelay(pdMS_TO_TICKS(1000));

        // 3.
        ret = 0;
        at_send_MQTTCONN("192.168.0.130");
        sprintf(func_name, "at_send_MQTTCONN");
        while ((ret = waiting_OK(10000)) != AT_ERRNO_OK) {
          switch (ret) {
            case AT_ERRNO_TIMEOUT:
              printf("%s timeout\r\n", func_name);
              break;
            case AT_ERRNO_ERROR:
              printf("%s error\r\n", func_name);
              break;
            default:
              printf("unknown ret:%d\r\n", ret);
              break;
          }
          vTaskDelay(pdMS_TO_TICKS(3000));
          at_send_MQTTCONN("192.168.0.130");
        }
        reset_wait_OK(&g_wait_ok); 
        vTaskDelay(pdMS_TO_TICKS(1000));

        // 4.
        at_send_MQTTSUB("attributes/push");
        sprintf(func_name, "at_send_MQTTSUB");
        while ((ret = waiting_OK(5000)) != AT_ERRNO_OK) {
          switch (ret) {
            case AT_ERRNO_TIMEOUT:
              printf("%s timeout\r\n", func_name);
              break;
            case AT_ERRNO_ERROR:
              printf("%s error\r\n", func_name);
              break;
            default:
              printf("unknown ret:%d\r\n", ret);
              break;
          }
          vTaskDelay(pdMS_TO_TICKS(3000));
          at_send_MQTTSUB("attributes/push");
        }
        reset_wait_OK(&g_wait_ok); 
        vTaskDelay(pdMS_TO_TICKS(1000));
        printf("************** sub finished **************\n");
        g_is_sub_topic = pdTRUE;
        // 5.对订阅主题内容进行处理
        TOPIC_BUFFER tbuf;
        while (1) {
          memset(&tbuf, 0, sizeof(TOPIC_BUFFER));
          if (xQueueReceive(iot_rx_topic_que, &tbuf, portMAX_DELAY) == pdTRUE) {
            printf("[%s] %s\n", tbuf.topic_, tbuf.data_);
            if (strcmp(tbuf.topic_, "attributes/push") == 0) {
              int val = get_relay_val_from_str(tbuf.data_);
            }
            // 释放内存
            if (tbuf.topic_ != NULL) {
              vPortFree(tbuf.topic_);
              tbuf.topic_ = NULL;
            }
            if (tbuf.data_ != NULL) {
              vPortFree(tbuf.data_);
              tbuf.data_ = NULL;
            }
          }
          vTaskDelay(pdMS_TO_TICKS(100));
        }
    }
}
